<?php
	// Make sure we're logged in (send us to the front page if not)
	gatekeeper();

  // Make sure action is secure
  action_gatekeeper();

	// Get input data
  $newlang = get_input('pellang');
  $access= get_input('access_id');
  if(is_array(get_input('champsSup'))){
    $champsSup = get_input('champsSup');
    foreach ($champsSup as $k => $v){
      $certificat[$k]= get_input('langcertificat_'.$k);
      $organisme[$k]= get_input('langorganisme_'.$k);
      $note[$k]=get_input('langnote_'.$k);
      $date[$k]=get_input('langdate_'.$k);
    } 
  }
  if(is_array(get_input('champsSupTravel'))){
    $champsSupTravel = get_input('champsSupTravel');
    foreach ($champsSupTravel as $k => $v){
      $travel[$k]= get_input('langtravel_'.$k);
      $traveldate[$k]= get_input('langtraveldate_'.$k);
    } 
  }
	if(is_array(get_input('champsSupTraining'))){
    $champsSupTraining= get_input('champsSupTraining');
    foreach ($champsSupTraining as $k => $v){
      $training[$k]= get_input('langtraining_'.$k);
      $trainingdate[$k]= get_input('langtrainingdate_'.$k);
    } 
  }

  $ecouter = get_input('ecouter');
  $lire = get_input('lire');
  $conversation = get_input('conversation');
  $expression = get_input('exprimer');
  $ecrire = get_input('ecrire');
  $media= get_input('langmedia');
      
// Cache to the session
  $_SESSION['pellang'] = $newlang;
  
// Make sure the title / description aren't blank
  if (empty($newlang)){
    register_error(elgg_echo("pel:blank"));
    forward("mod/pel/addlang.php");
  }
  else{
    // Initialise a new ElggObject
    $lang = new ElggObject();
    // Tell the system it's a pel language
    $lang->subtype = "lang";
    // Set its owner to the current user
    $lang->owner_guid = $_SESSION['user']->getGUID();

    // Set its description appropriately
		$lang->title ='Langue '.$newlang . ' de '. $_SESSION['user']->name;
    $lang->description = $newlang;
    $lang->access_id = $access;
    // Before we can set metadata, we need to save the pel language
    if (!$lang->save()){      
			register_error(elgg_echo("pel:error"));
      forward("mod/pel/addlang.php");
    }
		
    $pelParent= $_SESSION['user']->getObjects('pel');
    $lang->parent_guid = $pelParent[0]->getGUID();
    $lang->ecouter=$ecouter;
    $lang->lire=$lire;
    $lang->conversation=$conversation;
    $lang->expression=$expression;
    $lang->ecrire=$ecrire;
    $lang->travel=$travel;
    $lang->training=$training;
    $lang->traveldate=$traveldate;
    $lang->trainingdate=$trainingdate;
    $lang->media=$media;
    $lang->certificat=$certificat;
    $lang->organisme=$organisme;
    $lang->note=$note;
    $lang->date=$date;
  
    // Deal with the file
  /*$file= new ElggFile();
  $file->owner_guid = $_SESSION['user']->getGUID();
  $file->container_guid = $lang->getGUID();
  $file->setFilename("lang/" . $_SESSION['user']->username . time() .$_FILES['file_upload']['name']);
  $file->setMimeType($_FILES['file_upload']['type']);
  $file->originalfilename = $_FILES['file_upload']['name'];
  $file->open("write");
  $file->write(get_uploaded_file('file_upload'));
  $file->close();
  $file->save();
  $lang->file=$file->getFilename();*/
	  //add_to_river('river/object/pel/create','create',$_SESSION['user']->guid,$lang->guid);

    // Success message
    system_message(elgg_echo("pel:saved"));
    // Remove the pel language cache
    unset($_SESSION['pellang']);
    // Forward to the main pel page
    forward("mod/pel/?username=" . $_SESSION['user']->username);
  }
?>
